

#include <sudoku.h>

#include <stdio.h>
#include <string.h>

extern void _sdk_print_sudoku_horiz_div();
extern void _sdk_print_sudoku_pos_lists(sudoku *);
extern void _sdk_print_sudoku_all_pos_lists(sudoku *);

int main(int argc, char **argv){

  printf("sudosolve: version=%s\n", _SDK_VERSION);
  printf("  Sudoku format: %dx%d\n", SDK_WIDTH, SDK_WIDTH);
  printf("  CPU name: %s\n", _SDK_PROC);
  printf("  CPU architecure: %s\n", _SDK_ARCH);
#ifdef _SDK_PROC_x86_64
  printf("  Using 64 bit: yes\n");
#else
  printf("  Using 64 bit: no\n");
#endif

  /* Build cpu flags string. */
  char cpu_flags[64]; // Have to be careful with this...                        
  memset(cpu_flags, 0, 64);
#ifdef _SDK_HAVE_MMX 
  strcat(cpu_flags, "mmx "); // Not actually needed.
#endif
#ifdef _SDK_HAVE_SSE
  strcat(cpu_flags, "sse "); // Not actually needed.
#endif
#ifdef _SDK_HAVE_SSE2
  strcat(cpu_flags, "sse2 "); // Not actually needed.
#endif
#ifdef _SDK_HAVE_SSE3
  strcat(cpu_flags, "sse3 "); // Not actually needed.
#endif
#ifdef _SDK_HAVE_3DNow
  strcat(cpu_flags, "3DNow! "); // Not actually needed.
#endif

  printf("  Processor Flags: %s\n", cpu_flags);

#ifdef _DEBUG
  printf(" ** Compiled with debug on. **\n");
#endif

  printf("Memory usage: %d\n", sdk_memory_usage());

  sudoku *sdk = sdk_load_sudoku(argv[1]);
  if (sdk == NULL){
    printf("Error loading sudoku: %s; exiting.\n", argv[1]);
    exit(1);
  }

  printf("Loaded memory usage: %d\n", sdk_memory_usage());
  sdk_generate_lists(sdk);
  printf("Loaded (with lists) memory usage: %d\n", sdk_memory_usage());

  printf("Solving sudoku...\n");
  sudoku *solved = sdk_solve(sdk);
  if (solved) sdk_print_sudoku(solved);
  else printf("The sudoku appears to be unsolvable.\n");

  if (sdk == solved){
    SDK_FREE_SUDOKU(sdk);
  } else {
    SDK_FREE_SUDOKU(sdk);
    SDK_FREE_SUDOKU(solved);
  }
  printf("After freeing the sudokus: %d\n", sdk_memory_usage());

  /* Find a branch square on the copied sudoku. */
  //sdk_print_sudoku(sdk);
  //sdk_square *branch = _sdk_find_branch_square(sdk);
  //sdk_print_sudoku_square(branch);

  printf("sizeof(sudoku): %d\n", sizeof(sudoku));
  printf("sizeof(sdk_square): %d\n", sizeof(sdk_square));
  printf("sizeof(sdk_llist_t): %d\n", sizeof(sdk_llist_t));

}
